package com.zmn.oms.api.controller.third.yajule;

import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.third.yajule.YajuleConsts;
import com.zmn.oms.third.yajule.service.YajuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * 类描述：雅居乐渠道对接接口
 *  用于雅居乐渠道平台推送支付结果
 *
 * @author XieWenBing
 * @date 2020/08/04 15:00
 */

@Controller
@RequestMapping("/yajule")
@Slf4j
public class YajuleController {

    @Autowired
    private YajuleService yajuleService;

    @RequestMapping("/api/syncOrderPay")
    @ResponseBody
    public int syncOrderPayResult(@NotNull @RequestParam String resultData) {

        log.info("【{}】收到支付结果消息，消息:【{}】" ,YajuleConsts.CHANNEL_NAME,resultData);

        int resultCode ;

        try {
            resultCode = yajuleService.handlePayResultData(resultData);
        } catch (OmsBaseException e) {

            log.info("【{}】处理支付结果异常，OmsBaseException:【{}】",YajuleConsts.CHANNEL_NAME,
                    Objects.nonNull(e) ? e.getMessage():"未知异常");
            resultCode = Objects.nonNull(e) ? e.getCode():-1;
        } catch (Exception e) {

            log.info("【{}】处理支付结果异常，Exception:【{}】",YajuleConsts.CHANNEL_NAME,
                    Objects.nonNull(e) ? e.getMessage():"未知异常");
            resultCode = -1;
        }

        return resultCode;
    }
}
